š Problem Statement
You are part of a biomedical research team studying genetic marker patterns in patients over time. Each day, advanced diagnostic tools measure a genetic signal intensity, a numerical value representing the presence and dominance of certain genetic markers in a patient's blood sample.
These readings are stored as an array of integers for N consecutive days for each patient. Genetic researchers frequently submit queries like: What is the GCD of all signal values in a given period that are divisible by a specific marker threshold K.
š„ Input Format
- First line:
N(number of days, 1 ⤠N ⤠20) - Second line:
Nspace-separated integers (signal values, 1 ⤠Signal[i] ⤠200) - Third line:
Q(number of queries, 1 ⤠Q ⤠20) - Next
Qlines: Three integersL,R, andK- L, R: range of days (0 ⤠L ⤠R < N)
- K: threshold - only values divisible by K are considered (1 ⤠K ⤠100)
š¤ Output Format
For each query, output the GCD of all values in range [L, R] that are divisible by K.
If no value in the range is divisible by K, output 0.
šÆ Sample Test Case 1
6 10 15 20 25 30 35 3 0 3 5 1 5 10 2 4 3
5 10 30
- Query [0, 3] with K=5:
- Range: {10, 15, 20, 25}
- Divisible by 5: {10, 15, 20, 25} (all)
- GCD(10, 15, 20, 25) = 5
- Query [1, 5] with K=10:
- Range: {15, 20, 25, 30, 35}
- Divisible by 10: {20, 30}
- GCD(20, 30) = 10
- Query [2, 4] with K=3:
- Range: {20, 25, 30}
- Divisible by 3: {30}
- GCD(30) = 30
šÆ Sample Test Case 2
7 12 18 24 30 36 42 48 3 0 6 6 2 5 3 1 4 5
6 6 30
š§ Sparse Table for Filtered GCD Queries
This problem combines range querying with filtering. We need to find the GCD of values in a range that satisfy a divisibility condition. While sparse tables work well for GCD (since GCD is associative), the filtering adds complexity.
š Approach
Since the threshold K varies per query and we can't precompute for all possible K values efficiently, we use a hybrid approach:
- Build a sparse table for standard range GCD queries
- For each query, filter values in the range that are divisible by K
- Compute GCD of the filtered values
š§ Building Sparse Table (for GCD)
// GCD function
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// Initialize for length 1 (2^0)
for (int i = 0; i < n; i++) {
sparse[i][0] = signal[i];
}
// Build for increasing powers of 2
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
sparse[i][j] = gcd(sparse[i][j-1],
sparse[i + (1 << (j-1))][j-1]);
}
}
š Query Processing
int queryFilteredGCD(int L, int R, int K) {
vector filtered;
// Filter values divisible by K
for (int i = L; i <= R; i++) {
if (signal[i] % K == 0) {
filtered.push_back(signal[i]);
}
}
// If no values match, return 0
if (filtered.empty()) {
return 0;
}
// Compute GCD of filtered values
int result = filtered[0];
for (int i = 1; i < filtered.size(); i++) {
result = gcd(result, filtered[i]);
if (result == 1) break; // Optimization
}
return result;
}
ā” Complexity Analysis
- Build: O(n log n) - construct sparse table
- Query: O(n) to filter + O(m log V) to compute GCD of m filtered values
- Note: The filtering step dominates, making this O(n) per query
š” GCD Properties
- Associative: GCD(a, GCD(b, c)) = GCD(GCD(a, b), c)
- Commutative: GCD(a, b) = GCD(b, a)
- Identity: GCD(a, 0) = a
- Divisibility: If all numbers are divisible by K, their GCD is also divisible by K
- Range Property: GCD of numbers ⤠minimum number in the set
šÆ Example Walkthrough
Signals: [10, 15, 20, 25, 30], Query [0, 4] with K=5
Step 1: Filter values divisible by 5 10 % 5 = 0 ā ā Include 10 15 % 5 = 0 ā ā Include 15 20 % 5 = 0 ā ā Include 20 25 % 5 = 0 ā ā Include 25 30 % 5 = 0 ā ā Include 30 Filtered: [10, 15, 20, 25, 30] Step 2: Compute GCD iteratively GCD(10, 15) = 5 GCD(5, 20) = 5 GCD(5, 25) = 5 GCD(5, 30) = 5 Result: 5